home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-14 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  51KB  |  938 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Classifying Events,  Next: Accessing Events,  Prev: Event Examples,  Up: Input Events
  20. Classifying Events
  21. ------------------
  22.    Every event has an "event type" which classifies the event for key
  23. binding purposes.  For a keyboard event, the event type equals the event
  24. value; thus, the event type for a character is the character, and the
  25. event type for a function key symbol is the symbol itself.  For events
  26. which are lists, the event type is the symbol in the CAR of the list.
  27. Thus, the event type is always a symbol or a character.
  28.    Two events of the same type are equivalent where key bindings are
  29. concerned; thus, they always run the same command.  That does not
  30. necessarily mean they do the same things, however, as some commands look
  31. at the whole event to decide what to do.  For example, some commands use
  32. the location of a mouse event to decide what text to act on.
  33.    Sometimes broader classifications of events are useful.  For example,
  34. you might want to ask whether an event involved the META key,
  35. regardless of which other key or mouse button was used.
  36.    To get such information conveniently, call the functions
  37. `event-modifiers' and `event-basic-type'.
  38.  - Function: event-modifiers EVENT
  39.      This function returns a list of the modifiers that EVENT has.  The
  40.      modifiers are symbols; they include `shift', `control', `meta',
  41.      `alt', `hyper' and `super'.  In addition, the property of a mouse
  42.      event symbol always has one of `click', `drag', and `down' among
  43.      the modifiers.  For example:
  44.           (event-modifiers ?a)
  45.                => nil
  46.           (event-modifiers ?\C-a)
  47.                => (control)
  48.           (event-modifiers ?\C-%)
  49.                => (control)
  50.           (event-modifiers ?\C-\S-a)
  51.                => (control shift)
  52.           (event-modifiers 'f5)
  53.                => nil
  54.           (event-modifiers 's-f5)
  55.                => (super)
  56.           (event-modifiers 'M-S-f5)
  57.                => (meta shift)
  58.           (event-modifiers 'mouse-1)
  59.                => (click)
  60.           (event-modifiers 'down-mouse-1)
  61.                => (down)
  62.      The modifiers list for a click event explicitly contains `click',
  63.      but the event symbol name itself does not contain `click'.
  64.  - Function: event-basic-type EVENT
  65.      This function returns the key or mouse button that EVENT
  66.      describes, with all modifiers removed.  For example:
  67.           (event-basic-type ?a)
  68.                => 97
  69.           (event-basic-type ?A)
  70.                => 97
  71.           (event-basic-type ?\C-a)
  72.                => 97
  73.           (event-basic-type ?\C-\S-a)
  74.                => 97
  75.           (event-basic-type 'f5)
  76.                => f5
  77.           (event-basic-type 's-f5)
  78.                => f5
  79.           (event-basic-type 'M-S-f5)
  80.                => f5
  81.           (event-basic-type 'down-mouse-1)
  82.                => mouse-1
  83.  - Function: mouse-movement-p OBJECT
  84.      This function returns non-`nil' if OBJECT is a mouse movement
  85.      event.
  86. File: elisp,  Node: Accessing Events,  Next: Strings of Events,  Prev: Classifying Events,  Up: Input Events
  87. Accessing Events
  88. ----------------
  89.    This section describes convenient functions for accessing the data in
  90. an event which is a list.
  91.    The following functions return the starting or ending position of a
  92. mouse-button event.  The position is a list of this form:
  93.      (WINDOW BUFFER-POSITION (COL . ROW) TIMESTAMP)
  94.  - Function: event-start EVENT
  95.      This returns the starting position of EVENT.
  96.      If EVENT is a click or button-down event, this returns the
  97.      location of the event.  If EVENT is a drag event, this returns the
  98.      drag's starting position.
  99.  - Function: event-end EVENT
  100.      This returns the ending position of EVENT.
  101.      If EVENT is a drag event, this returns the position where the user
  102.      released the mouse button.  If EVENT is a click or button-down
  103.      event, the value is actually the starting position, which is the
  104.      only position such events have.
  105.    These four functions take a position-list as described above, and
  106. return various parts of it.
  107.  - Function: posn-window POSITION
  108.      Return the window that POSITION is in.
  109.  - Function: posn-point POSITION
  110.      Return the buffer location in POSITION.
  111.  - Function: posn-col-row POSITION
  112.      Return the row and column in POSITION, as a list `(COL . ROW)'.
  113.  - Function: posn-timestamp POSITION
  114.      Return the timestamp of POSITION.
  115.  - Function: scroll-bar-scale RATIO TOTAL
  116.      This function multiples (in effect) RATIO by TOTAL, rounding the
  117.      result to an integer.  RATIO is not a number, but rather  a pair
  118.      `(NUM . DENOM)'.
  119.      This is handy for scaling a position on a scroll bar into a buffer
  120.      position.  Here's how to do that:
  121.           (scroll-bar-scale (posn-col-row (event-start event))
  122.                             (buffer-size))
  123. File: elisp,  Node: Strings of Events,  Prev: Accessing Events,  Up: Input Events
  124. Putting Keyboard Events in Strings
  125. ----------------------------------
  126.    In most of the places where strings are used, we conceptualize the
  127. string as containing text characters--the same kind of characters found
  128. in buffers or files.  Occasionally Lisp programs use strings which
  129. conceptually contain keyboard characters; for example, they may be key
  130. sequences or keyboard macro definitions.  There are special rules for
  131. how to put keyboard characters into a string, because they are not
  132. limited to the range of 0 to 255 as text characters are.
  133.    A keyboard character typed using the META key is called a "meta
  134. character".  The numeric code for such an event includes the 2**23 bit;
  135. it does not even come close to fitting in a string.  However, earlier
  136. Emacs versions used a different representation for these characters,
  137. which gave them codes in the range of 128 to 255.  That did fit in a
  138. string, and many Lisp programs contain string constants that use `\M-'
  139. to express meta characters, especially as the argument to `define-key'
  140. and similar functions.
  141.    We provide backward compatibility to run those programs with special
  142. rules for how to put a keyboard character event in a string.  Here are
  143. the rules:
  144.    * If the keyboard event value is in the range of 0 to 127, it can go
  145.      in the string unchanged.
  146.    * The meta variants of those events, with codes in the range of
  147.      2**23 to 2**23+127, can also go in the string, but you must change
  148.      their numeric values.  You must set the 2**7 bit instead of the
  149.      2**23 bit, resulting in a value between 128 and 255.
  150.    * Other keyboard character events cannot fit in a string.  This
  151.      includes keyboard events in the range of 128 to 255.
  152.    Functions such as `read-key-sequence' that can construct strings
  153. containing events follow these rules.
  154.    When you use the read syntax `\M-' in a string, it produces a code
  155. in the range of 128 to 255--the same code that you get if you modify
  156. the corresponding keyboard event to put it in the string.  Thus, meta
  157. events in strings work consistently regardless of how they get into the
  158. strings.
  159.    New programs can avoid dealing with these rules by using vectors
  160. instead of strings for key sequences when there is any possibility that
  161. these issues might arise.
  162.    The reason we changed the representation of meta characters as
  163. keyboard events is to make room for basic character codes beyond 127,
  164. and support meta variants of such larger character codes.
  165. File: elisp,  Node: Reading Input,  Next: Waiting,  Prev: Input Events,  Up: Command Loop
  166. Reading Input
  167. =============
  168.    The editor command loop reads keyboard input using the function
  169. `read-key-sequence', which uses `read-event'.  These and other
  170. functions for keyboard input are also available for use in Lisp
  171. programs.  See also `momentary-string-display' in *Note Temporary
  172. Displays::, and `sit-for' in *Note Waiting::.  *Note Terminal Input::,
  173. for functions and variables for controlling terminal input modes and
  174. debugging terminal input.
  175.    For higher-level input facilities, see *Note Minibuffers::.
  176. * Menu:
  177. * Key Sequence Input::        How to read one key sequence.
  178. * Reading One Event::        How to read just one event.
  179. * Quoted Character Input::    Asking the user to specify a character.
  180. * Peeking and Discarding::    How to reread or throw away input events.
  181. File: elisp,  Node: Key Sequence Input,  Next: Reading One Event,  Up: Reading Input
  182. Key Sequence Input
  183. ------------------
  184.    The command loop reads input a key sequence at a time, by calling
  185. `read-key-sequence'.  Lisp programs can also call this function; for
  186. example, `describe-key' uses it to read the key to describe.
  187.  - Function: read-key-sequence PROMPT
  188.      This function reads a key sequence and returns it as a string or
  189.      vector.  It keeps reading events until it has accumulated a full
  190.      key sequence; that is, enough to specify a non-prefix command
  191.      using the currently active keymaps.
  192.      If the events are all characters and all can fit in a string, then
  193.      `read-key-sequence' returns a string (*note Strings of Events::.).
  194.      Otherwise, it returns a vector, since a vector can hold all kinds
  195.      of events--characters, symbols, and lists.  The elements of the
  196.      string or vector are the events in the key sequence.
  197.      Quitting is suppressed inside `read-key-sequence'.  In other words,
  198.      a `C-g' typed while reading with this function is treated like any
  199.      other character, and does not set `quit-flag'.  *Note Quitting::.
  200.      The argument PROMPT is either a string to be displayed in the echo
  201.      area as a prompt, or `nil', meaning not to display a prompt.
  202.      In the example below, the prompt `?' is displayed in the echo area,
  203.      and the user types `C-x C-f'.
  204.           (read-key-sequence "?")
  205.           
  206.           ---------- Echo Area ----------
  207.           ?`C-x C-f'
  208.           ---------- Echo Area ----------
  209.           
  210.                => "^X^F"
  211.  - Variable: num-input-keys
  212.      This variable's value is the number of key sequences processed so
  213.      far in this Emacs session.  This includes key sequences read from
  214.      the terminal and key sequences read from keyboard macros being
  215.      executed.
  216.    If an input character is an upper case letter and has no key binding,
  217. but the lower case equivalent has one, then `read-key-sequence'
  218. converts the character to lower case.  Note that `lookup-key' does not
  219. perform case conversion in this way.
  220.    The function `read-key-sequence' also transforms some mouse events.
  221. It converts unbound drag events into click events, and discards unbound
  222. button-down events entirely.  It also reshuffles focus events so that
  223. they never appear in a key sequence with any other events.
  224.    When mouse events occur in special parts of a window, such as a mode
  225. line or a scroll bar, the event itself shows nothing special--only the
  226. symbol that would normally represent that mouse button and modifier
  227. keys.  The information about the screen region is kept elsewhere in the
  228. event--in the coordinates.  But `read-key-sequence' translates this
  229. information into imaginary prefix keys, all of which are symbols:
  230. `mode-line', `vertical-line', `horizontal-scroll-bar' and
  231. `vertical-scroll-bar'.
  232.    For example, if you call `read-key-sequence' and then click the
  233. mouse on the window's mode line, this is what happens:
  234.      (read-key-sequence "Click on the mode line: ")
  235.           => [mode-line
  236.                (mouse-1
  237.                 (#<window 6 on NEWS> mode-line
  238.                  (40 . 63) 5959987))]
  239.    You can define meanings for mouse clicks in special window regions by
  240. defining key sequences using these imaginary prefix keys.
  241. File: elisp,  Node: Reading One Event,  Next: Quoted Character Input,  Prev: Key Sequence Input,  Up: Reading Input
  242. Reading One Event
  243. -----------------
  244.    The lowest level functions for command input are those which read a
  245. single event.
  246.  - Function: read-event
  247.      This function reads and returns the next event of command input,
  248.      waiting if necessary until an event is available.  Events can come
  249.      directly from the user or from a keyboard macro.
  250.      The function `read-event' does not display any message to indicate
  251.      it is waiting for input; use `message' first, if you wish to
  252.      display one.  If you have not displayed a message, `read-event'
  253.      does "prompting": it displays descriptions of the events that led
  254.      to or were read by the current command.  *Note The Echo Area::.
  255.      If `cursor-in-echo-area' is non-`nil', then `read-event' moves the
  256.      cursor temporarily to the echo area, to the end of any message
  257.      displayed there.  Otherwise `read-event' does not move the cursor.
  258.    Here is what happens if you call `read-event' and then press the
  259. right-arrow function key:
  260.      (read-event)
  261.           => right
  262.  - Function: read-char
  263.      This function reads and returns a character of command input.  It
  264.      discards any events that are not characters until it gets a
  265.      character.
  266.      In the first example, the user types `1' (which is ASCII code 49).
  267.      The second example shows a keyboard macro definition that calls
  268.      `read-char' from the minibuffer.  `read-char' reads the keyboard
  269.      macro's very next character, which is `1'.  The value of this
  270.      function is displayed in the echo area by the command
  271.      `eval-expression'.
  272.           (read-char)
  273.                => 49
  274.           
  275.           (symbol-function 'foo)
  276.                => "^[^[(read-char)^M1"
  277.           (execute-kbd-macro foo)
  278.                -| 49
  279.                => nil
  280. File: elisp,  Node: Quoted Character Input,  Next: Peeking and Discarding,  Prev: Reading One Event,  Up: Reading Input
  281. Quoted Character Input
  282. ----------------------
  283.    You can use the function `read-quoted-char' when you want the user
  284. to specify a character, and allow the user to specify a control or meta
  285. character conveniently with quoting or as an octal character code.  The
  286. command `quoted-insert' calls this function.
  287.  - Function: read-quoted-char &optional PROMPT
  288.      This function is like `read-char', except that if the first
  289.      character read is an octal digit (0-7), it reads up to two more
  290.      octal digits (but stopping if a non-octal digit is found) and
  291.      returns the character represented by those digits as an octal
  292.      number.
  293.      Quitting is suppressed when the first character is read, so that
  294.      the user can enter a `C-g'.  *Note Quitting::.
  295.      If PROMPT is supplied, it specifies a string for prompting the
  296.      user.  The prompt string is always printed in the echo area and
  297.      followed by a single `-'.
  298.      In the following example, the user types in the octal number 177
  299.      (which is 127 in decimal).
  300.           (read-quoted-char "What character")
  301.           
  302.           ---------- Echo Area ----------
  303.           What character-`177'
  304.           ---------- Echo Area ----------
  305.           
  306.                => 127
  307. File: elisp,  Node: Peeking and Discarding,  Prev: Quoted Character Input,  Up: Reading Input
  308. Peeking and Discarding
  309. ----------------------
  310.  - Variable: unread-command-events
  311.      This variable holds a list of events waiting to be read as command
  312.      input.  The events are used in the order they appear in the list.
  313.      The variable is used because in some cases a function reads a
  314.      event and then decides not to use it.  Storing the event in this
  315.      variable causes it to be processed normally by the command loop or
  316.      when the functions to read command input are called.
  317.      For example, the function that implements numeric prefix arguments
  318.      reads any number of digits.  When it finds a non-digit event, it
  319.      must unread the event so that it can be read normally by the
  320.      command loop.  Likewise, incremental search uses this feature to
  321.      unread events it does not recognize.
  322.  - Variable: unread-command-char
  323.      This variable holds a character to be read as command input.  A
  324.      value of -1 means "empty".
  325.      This variable is pretty much obsolete now that you can use
  326.      `unread-command-events' instead; it exists only to support programs
  327.      written for Emacs versions 18 and earlier.
  328.  - Function: listify-key-sequence KEY
  329.      This function converts the string or vector KEY to a list of
  330.      events which you can put in `unread-command-events'.  Converting a
  331.      vector is simple, but converting a string is tricky because of the
  332.      special representation used for meta characters in a string (*note
  333.      Strings of Events::.).
  334.  - Function: input-pending-p
  335.      This function determines whether any command input is currently
  336.      available to be read.  It returns immediately, with value `t' if
  337.      there is input, `nil' otherwise.  On rare occasions it may return
  338.      `t' when no input is available.
  339.  - Variable: last-input-event
  340.  - Variable: last-input-char
  341.      This variable records the last terminal input event read, whether
  342.      as part of a command or explicitly by a Lisp program.
  343.      In the example below, a character is read (the character `1',
  344.      ASCII code 49).  It becomes the value of `last-input-char', while
  345.      `C-e' (from the `C-x C-e' command used to evaluate this
  346.      expression) remains the value of `last-command-char'.
  347.           (progn (print (read-char))
  348.                  (print last-command-char)
  349.                  last-input-char)
  350.                -| 49
  351.                -| 5
  352.                => 49
  353.      The alias `last-input-char' exists for compatibility with Emacs
  354.      version 18.
  355.  - Function: discard-input
  356.      This function discards the contents of the terminal input buffer
  357.      and cancels any keyboard macro that might be in the process of
  358.      definition.  It returns `nil'.
  359.      In the following example, the user may type a number of characters
  360.      right after starting the evaluation of the form.  After the
  361.      `sleep-for' finishes sleeping, any characters that have been typed
  362.      are discarded.
  363.           (progn (sleep-for 2)
  364.             (discard-input))
  365.                => nil
  366. File: elisp,  Node: Waiting,  Next: Quitting,  Prev: Reading Input,  Up: Command Loop
  367. Waiting for Elapsed Time or Input
  368. =================================
  369.    The waiting commands are designed to make Emacs wait for a certain
  370. amount of time to pass or until there is input.  For example, you may
  371. wish to pause in the middle of a computation to allow the user time to
  372. view the display.  `sit-for' pauses and updates the screen, and returns
  373. immediately if input comes in, while `sleep-for' pauses without
  374. updating the screen.
  375.  - Function: sit-for SECONDS &optional MILLISEC NODISP
  376.      This function performs redisplay (provided there is no pending
  377.      input from the user), then waits SECONDS seconds, or until input is
  378.      available.  The result is `t' if `sit-for' waited the full time
  379.      with no input arriving (see `input-pending-p' in *Note Peeking and
  380.      Discarding::).  Otherwise, the value is `nil'.
  381.      The optional argument MILLISEC specifies an additional waiting
  382.      period measured in milliseconds.  This adds to the period
  383.      specified by SECONDS.  Not all operating systems support waiting
  384.      periods other than multiples of a second; on those that do not,
  385.      you get an error if you specify nonzero MILLISEC.
  386.      Redisplay is always preempted if input arrives, and does not
  387.      happen at all if input is available before it starts.  Thus, there
  388.      is no way to force screen updating if there is pending input;
  389.      however, if there is no input pending, you can force an update
  390.      with no delay by using `(sit-for 0)'.
  391.      If NODISP is non-`nil', then `sit-for' does not redisplay, but it
  392.      still returns as soon as input is available (or when the timeout
  393.      elapses).
  394.      The usual purpose of `sit-for' is to give the user time to read
  395.      text that you display.
  396.  - Function: sleep-for SECONDS &optional MILLISEC
  397.      This function simply pauses for SECONDS seconds without updating
  398.      the display.  It pays no attention to available input.  It returns
  399.      `nil'.
  400.      The optional argument MILLISEC specifies an additional waiting
  401.      period measured in milliseconds.  This adds to the period
  402.      specified by SECONDS.  Not all operating systems support waiting
  403.      periods other than multiples of a second; on those that do not,
  404.      you get an error if you specify nonzero MILLISEC.
  405.      Use `sleep-for' when you wish to guarantee a delay.
  406.    *Note Time of Day::, for functions to get the current time.
  407. File: elisp,  Node: Quitting,  Next: Prefix Command Arguments,  Prev: Waiting,  Up: Command Loop
  408. Quitting
  409. ========
  410.    Typing `C-g' while the command loop has run a Lisp function causes
  411. Emacs to "quit" whatever it is doing.  This means that control returns
  412. to the innermost active command loop.
  413.    Typing `C-g' while the command loop is waiting for keyboard input
  414. does not cause a quit; it acts as an ordinary input character.  In the
  415. simplest case, you cannot tell the difference, because `C-g' normally
  416. runs the command `keyboard-quit', whose effect is to quit.  However,
  417. when `C-g' follows a prefix key, the result is an undefined key.  The
  418. effect is to cancel the prefix key as well as any prefix argument.
  419.    In the minibuffer, `C-g' has a different definition: it aborts out
  420. of the minibuffer.  This means, in effect, that it exits the minibuffer
  421. and then quits.  (Simply quitting would return to the command loop
  422. *within* the minibuffer.)  The reason why `C-g' does not quit directly
  423. when the command reader is reading input is so that its meaning can be
  424. redefined in the minibuffer in this way.  `C-g' following a prefix key
  425. is not redefined in the minibuffer, and it has its normal effect of
  426. canceling the prefix key and prefix argument.  This too would not be
  427. possible if `C-g' quit directly.
  428.    `C-g' causes a quit by setting the variable `quit-flag' to a
  429. non-`nil' value.  Emacs checks this variable at appropriate times and
  430. quits if it is not `nil'.  Setting `quit-flag' non-`nil' in any way
  431. thus causes a quit.
  432.    At the level of C code, quits cannot happen just anywhere; only at
  433. the special places which check `quit-flag'.  The reason for this is
  434. that quitting at other places might leave an inconsistency in Emacs's
  435. internal state.  Because quitting is delayed until a safe place,
  436. quitting cannot make Emacs crash.
  437.    Certain functions such as `read-key-sequence' or `read-quoted-char'
  438. prevent quitting entirely even though they wait for input.  Instead of
  439. quitting, `C-g' serves as the requested input.  In the case of
  440. `read-key-sequence', this serves to bring about the special behavior of
  441. `C-g' in the command loop.  In the case of `read-quoted-char', this is
  442. so that `C-q' can be used to quote a `C-g'.
  443.    You can prevent quitting for a portion of a Lisp function by binding
  444. the variable `inhibit-quit' to a non-`nil' value.  Then, although `C-g'
  445. still sets `quit-flag' to `t' as usual, the usual result of this--a
  446. quit--is prevented.  Eventually, `inhibit-quit' will become `nil'
  447. again, such as when its binding is unwound at the end of a `let' form.
  448. At that time, if `quit-flag' is still non-`nil', the requested quit
  449. happens immediately.  This behavior is ideal for a "critical section",
  450. where you wish to make sure that quitting does not happen within that
  451. part of the program.
  452.    In some functions (such as `read-quoted-char'), `C-g' is handled in
  453. a special way which does not involve quitting.  This is done by reading
  454. the input with `inhibit-quit' bound to `t' and setting `quit-flag' to
  455. `nil' before `inhibit-quit' becomes `nil' again.  This excerpt from the
  456. definition of `read-quoted-char' shows how this is done; it also shows
  457. that normal quitting is permitted after the first character of input.
  458.      (defun read-quoted-char (&optional prompt)
  459.        "...DOCUMENTATION..."
  460.        (let ((count 0) (code 0) char)
  461.          (while (< count 3)
  462.            (let ((inhibit-quit (zerop count))
  463.                  (help-form nil))
  464.              (and prompt (message "%s-" prompt))
  465.              (setq char (read-char))
  466.              (if inhibit-quit (setq quit-flag nil)))
  467.            ...)
  468.          (logand 255 code)))
  469.  - Variable: quit-flag
  470.      If this variable is non-`nil', then Emacs quits immediately,
  471.      unless `inhibit-quit' is non-`nil'.  Typing `C-g' sets `quit-flag'
  472.      non-`nil', regardless of `inhibit-quit'.
  473.  - Variable: inhibit-quit
  474.      This variable determines whether Emacs should quit when `quit-flag'
  475.      is set to a value other than `nil'.  If `inhibit-quit' is
  476.      non-`nil', then `quit-flag' has no special effect.
  477.  - Command: keyboard-quit
  478.      This function signals the `quit' condition with `(signal 'quit
  479.      nil)'.  This is the same thing that quitting does.  (See `signal'
  480.      in *Note Errors::.)
  481.    You can specify a character other than `C-g' to use for quitting.
  482. See the function `set-input-mode' in *Note Terminal Input::.
  483. File: elisp,  Node: Prefix Command Arguments,  Next: Recursive Editing,  Prev: Quitting,  Up: Command Loop
  484. Prefix Command Arguments
  485. ========================
  486.    Most Emacs commands can use a "prefix argument", a number specified
  487. before the command itself.  (Don't confuse prefix arguments with prefix
  488. keys.)  The prefix argument is represented by a value that is always
  489. available (though it may be `nil', meaning there is no prefix
  490. argument).  Each command may use the prefix argument or ignore it.
  491.    There are two representations of the prefix argument: "raw" and
  492. "numeric".  The editor command loop uses the raw representation
  493. internally, and so do the Lisp variables that store the information, but
  494. commands can request either representation.
  495.    Here are the possible values of a raw prefix argument:
  496.    * `nil', meaning there is no prefix argument.  Its numeric value is
  497.      1, but numerous commands make a distinction between `nil' and the
  498.      integer 1.
  499.    * An integer, which stands for itself.
  500.    * A list of one element, which is an integer.  This form of prefix
  501.      argument results from one or a succession of `C-u''s with no
  502.      digits.  The numeric value is the integer in the list, but some
  503.      commands make a distinction between such a list and an integer
  504.      alone.
  505.    * The symbol `-'.  This indicates that `M--' or `C-u -' was typed,
  506.      without following digits.  The equivalent numeric value is -1, but
  507.      some commands make a distinction between the integer -1 and the
  508.      symbol `-'.
  509.    The various possibilities may be illustrated by calling the following
  510. function with various prefixes:
  511.      (defun display-prefix (arg)
  512.        "Display the value of the raw prefix arg."
  513.        (interactive "P")
  514.        (message "%s" arg))
  515. Here are the results of calling `print-prefix' with various raw prefix
  516. arguments:
  517.              M-x print-prefix  -| nil
  518.      
  519.      C-u     M-x print-prefix  -| (4)
  520.      
  521.      C-u C-u M-x print-prefix  -| (16)
  522.      
  523.      C-u 3   M-x print-prefix  -| 3
  524.      
  525.      M-3     M-x print-prefix  -| 3      ; (Same as `C-u 3'.)
  526.      
  527.      C-u -   M-x print-prefix  -| -
  528.      
  529.      M- -    M-x print-prefix  -| -      ; (Same as `C-u -'.)
  530.      
  531.      C-u -7  M-x print-prefix  -| -7
  532.      
  533.      M- -7   M-x print-prefix  -| -7     ; (Same as `C-u -7'.)
  534.    Emacs uses two variables to store the prefix argument: `prefix-arg'
  535. and `current-prefix-arg'.  Commands such as `universal-argument' that
  536. set up prefix arguments for other commands store them in `prefix-arg'.
  537. In contrast, `current-prefix-arg' conveys the prefix argument to the
  538. current command, so setting it has no effect on the prefix arguments
  539. for future commands.
  540.    Normally, commands specify which representation to use for the prefix
  541. argument, either numeric or raw, in the `interactive' declaration.
  542. (*Note Interactive Call::.)  Alternatively, functions may look at the
  543. value of the prefix argument directly in the variable
  544. `current-prefix-arg', but this is less clean.
  545.    Do not call the functions `universal-argument', `digit-argument', or
  546. `negative-argument' unless you intend to let the user enter the prefix
  547. argument for the *next* command.
  548.  - Command: universal-argument
  549.      This command reads input and specifies a prefix argument for the
  550.      following command.  Don't call this command yourself unless you
  551.      know what you are doing.
  552.  - Command: digit-argument ARG
  553.      This command adds to the prefix argument for the following
  554.      command.  The argument ARG is the raw prefix argument as it was
  555.      before this command; it is used to compute the updated prefix
  556.      argument.  Don't call this command yourself unless you know what
  557.      you are doing.
  558.  - Command: negative-argument ARG
  559.      This command adds to the numeric argument for the next command.
  560.      The argument ARG is the raw prefix argument as it was before this
  561.      command; its value is negated to form the new prefix argument.
  562.      Don't call this command yourself unless you know what you are
  563.      doing.
  564.  - Function: prefix-numeric-value ARG
  565.      This function returns the numeric meaning of a valid raw prefix
  566.      argument value, ARG.  The argument may be a symbol, a number, or a
  567.      list.  If it is `nil', the value 1 is returned; if it is any other
  568.      symbol, the value -1 is returned.  If it is a number, that number
  569.      is returned; if it is a list, the CAR of that list (which should
  570.      be a number) is returned.
  571.  - Variable: current-prefix-arg
  572.      This variable is the value of the raw prefix argument for the
  573.      *current* command.  Commands may examine it directly, but the usual
  574.      way to access it is with `(interactive "P")'.
  575.  - Variable: prefix-arg
  576.      The value of this variable is the raw prefix argument for the
  577.      *next* editing command.  Commands that specify prefix arguments for
  578.      the following command work by setting this variable.
  579. File: elisp,  Node: Recursive Editing,  Next: Disabling Commands,  Prev: Prefix Command Arguments,  Up: Command Loop
  580. Recursive Editing
  581. =================
  582.    The Emacs command loop is entered automatically when Emacs starts up.
  583. This top-level invocation of the command loop is never exited until the
  584. Emacs is killed.  Lisp programs can also invoke the command loop.  Since
  585. this makes more than one activation of the command loop, we call it
  586. "recursive editing".  A recursive editing level has the effect of
  587. suspending whatever command invoked it and permitting the user to do
  588. arbitrary editing before resuming that command.
  589.    The commands available during recursive editing are the same ones
  590. available in the top-level editing loop and defined in the keymaps.
  591. Only a few special commands exit the recursive editing level; the others
  592. return to the recursive editing level when finished.  (The special
  593. commands for exiting are always available, but do nothing when recursive
  594. editing is not in progress.)
  595.    All command loops, including recursive ones, set up all-purpose error
  596. handlers so that an error in a command run from the command loop will
  597. not exit the loop.
  598.    Minibuffer input is a special kind of recursive editing.  It has a
  599. few special wrinkles, such as enabling display of the minibuffer and the
  600. minibuffer window, but fewer than you might suppose.  Certain keys
  601. behave differently in the minibuffer, but that is only because of the
  602. minibuffer's local map; if you switch windows, you get the usual Emacs
  603. commands.
  604.    To invoke a recursive editing level, call the function
  605. `recursive-edit'.  This function contains the command loop; it also
  606. contains a call to `catch' with tag `exit', which makes it possible to
  607. exit the recursive editing level by throwing to `exit' (*note Catch and
  608. Throw::.).  If you throw a value other than `t', then `recursive-edit'
  609. returns normally to the function that called it.  The command `C-M-c'
  610. (`exit-recursive-edit') does this.  Throwing a `t' value causes
  611. `recursive-edit' to quit, so that control returns to the command loop
  612. one level up.  This is called "aborting", and is done by `C-]'
  613. (`abort-recursive-edit').
  614.    Most applications should not use recursive editing, except as part of
  615. using the minibuffer.  Usually it is more convenient for the user if you
  616. change the major mode of the current buffer temporarily to a special
  617. major mode, which has a command to go back to the previous mode.  (This
  618. technique is used by the `w' command in Rmail.)  Or, if you wish to
  619. give the user different text to edit "recursively", create and select a
  620. new buffer in a special mode.  In this mode, define a command to
  621. complete the processing and go back to the previous buffer.  (The `m'
  622. command in Rmail does this.)
  623.    Recursive edits are useful in debugging.  You can insert a call to
  624. `debug' into a function definition as a sort of breakpoint, so that you
  625. can look around when the function gets there.  `debug' invokes a
  626. recursive edit but also provides the other features of the debugger.
  627.    Recursive editing levels are also used when you type `C-r' in
  628. `query-replace' or use `C-x q' (`kbd-macro-query').
  629.  - Function: recursive-edit
  630.      This function invokes the editor command loop.  It is called
  631.      automatically by the initialization of Emacs, to let the user begin
  632.      editing.  When called from a Lisp program, it enters a recursive
  633.      editing level.
  634.      In the following example, the function `simple-rec' first advances
  635.      point one word, then enters a recursive edit, printing out a
  636.      message in the echo area.  The user can then do any editing
  637.      desired, and then type `C-M-c' to exit and continue executing
  638.      `simple-rec'.
  639.           (defun simple-rec ()
  640.             (forward-word 1)
  641.             (message "Recursive edit in progress.")
  642.             (recursive-edit)
  643.             (forward-word 1))
  644.                => simple-rec
  645.           (simple-rec)
  646.                => nil
  647.  - Command: exit-recursive-edit
  648.      This function exits from the innermost recursive edit (including
  649.      minibuffer input).  Its definition is effectively `(throw 'exit
  650.      nil)'.
  651.  - Command: abort-recursive-edit
  652.      This function aborts the command that requested the innermost
  653.      recursive edit (including minibuffer input), by signaling `quit'
  654.      after exiting the recursive edit.  Its definition is effectively
  655.      `(throw 'exit t)'.  *Note Quitting::.
  656.  - Command: top-level
  657.      This function exits all recursive editing levels; it does not
  658.      return a value, as it jumps completely out of any computation
  659.      directly back to the main command loop.
  660.  - Function: recursion-depth
  661.      This function returns the current depth of recursive edits.  When
  662.      no recursive edit is active, it returns 0.
  663. File: elisp,  Node: Disabling Commands,  Next: Command History,  Prev: Recursive Editing,  Up: Command Loop
  664. Disabling Commands
  665. ==================
  666.    "Disabling a command" marks the command as requiring user
  667. confirmation before it can be executed.  Disabling is used for commands
  668. which might be confusing to beginning users, to prevent them from using
  669. the commands by accident.
  670.    The low-level mechanism for disabling a command is to put a
  671. non-`nil' `disabled' property on the Lisp symbol for the command.
  672. These properties are normally set up by the user's `.emacs' file with
  673. Lisp expressions such as this:
  674.      (put 'upcase-region 'disabled t)
  675. For a few commands, these properties are present by default and may be
  676. removed by the `.emacs' file.
  677.    If the value of the `disabled' property is a string, that string is
  678. included in the message printed when the command is used:
  679.      (put 'delete-region 'disabled
  680.           "Text deleted this way cannot be yanked back!\n")
  681.    *Note Disabling: (emacs)Disabling, for the details on what happens
  682. when a disabled command is invoked interactively.  Disabling a command
  683. has no effect on calling it as a function from Lisp programs.
  684.  - Command: enable-command COMMAND
  685.      Allow COMMAND to be executed without special confirmation from now
  686.      on.  The user's `.emacs' file is optionally altered so that this
  687.      will apply to future sessions.
  688.  - Command: disable-command COMMAND
  689.      Require special confirmation to execute COMMAND from now on.  The
  690.      user's `.emacs' file is optionally altered so that this will apply
  691.      to future sessions.
  692.  - Variable: disabled-command-hook
  693.      This variable is a normal hook that is run instead of a disabled
  694.      command, when the user runs the disabled command interactively.
  695.      The hook functions can use `this-command-keys' to determine what
  696.      the user typed to run the command, and thus find the command
  697.      itself.
  698.      By default, `disabled-command-hook' contains a function that asks
  699.      the user whether to proceed.
  700. File: elisp,  Node: Command History,  Next: Keyboard Macros,  Prev: Disabling Commands,  Up: Command Loop
  701. Command History
  702. ===============
  703.    The command loop keeps a history of the complex commands that have
  704. been executed, to make it convenient to repeat these commands.  A
  705. "complex command" is one for which the interactive argument reading
  706. uses the minibuffer.  This includes any `M-x' command, any `M-ESC'
  707. command, and any command whose `interactive' specification reads an
  708. argument from the minibuffer.  Explicit use of the minibuffer during
  709. the execution of the command itself does not cause the command to be
  710. considered complex.
  711.  - Variable: command-history
  712.      This variable's value is a list of recent complex commands, each
  713.      represented as a form to evaluate.  It continues to accumulate all
  714.      complex commands for the duration of the editing session, but all
  715.      but the first (most recent) thirty elements are deleted when a
  716.      garbage collection takes place (*note Garbage Collection::.).
  717.           command-history
  718.           => ((switch-to-buffer "chistory.texi")
  719.               (describe-key "^X^[")
  720.               (visit-tags-table "~/emacs/src/")
  721.               (find-tag "repeat-complex-command"))
  722.    This history list is actually a special case of minibuffer history
  723. (*note Minibuffer History::.), with one special twist: the elements are
  724. expressions rather than strings.
  725.    There are a number of commands devoted to the editing and recall of
  726. previous commands.  The commands `repeat-complex-command', and
  727. `list-command-history' are described in the user manual (*note
  728. Repetition: (emacs)Repetition.).  Within the minibuffer, the history
  729. commands used are the same ones available in any minibuffer.
  730. File: elisp,  Node: Keyboard Macros,  Prev: Command History,  Up: Command Loop
  731. Keyboard Macros
  732. ===============
  733.    A "keyboard macro" is a canned sequence of input events that can be
  734. considered a command and made the definition of a key.  Don't confuse
  735. keyboard macros with Lisp macros (*note Macros::.).
  736.  - Function: execute-kbd-macro MACRO &optional COUNT
  737.      This function executes MACRO as a sequence of events.  If MACRO is
  738.      a string or vector, then the events in it are executed exactly as
  739.      if they had been input by the user.  The sequence is *not*
  740.      expected to be a single key sequence; normally a keyboard macro
  741.      definition consists of several key sequences concatenated.
  742.      If MACRO is a symbol, then its function definition is used in
  743.      place of MACRO.  If that is another symbol, this process repeats.
  744.      Eventually the result should be a string or vector.  If the result
  745.      is not a symbol, string, or vector, an error is signaled.
  746.      The argument COUNT is a repeat count; MACRO is executed that many
  747.      times.  If COUNT is omitted or `nil', MACRO is executed once.  If
  748.      it is 0, MACRO is executed over and over until it encounters an
  749.      error or a failing search.
  750.  - Variable: last-kbd-macro
  751.      This variable is the definition of the most recently defined
  752.      keyboard macro.  Its value is a string or vector, or `nil'.
  753.  - Variable: executing-macro
  754.      This variable contains the string or vector that defines the
  755.      keyboard macro that is currently executing.  It is `nil' if no
  756.      macro is currently executing.
  757.  - Variable: defining-kbd-macro
  758.      This variable indicates whether a keyboard macro is being defined.
  759.      It is set to `t' by `start-kbd-macro', and `nil' by
  760.      `end-kbd-macro'.  You can use this variable to make a command
  761.      behave differently when run from a keyboard macro (perhaps
  762.      indirectly by calling `interactive-p').  However, do not set this
  763.      variable yourself.
  764.    The commands are described in the user's manual (*note Keyboard
  765. Macros: (emacs)Keyboard Macros.).
  766. File: elisp,  Node: Keymaps,  Next: Modes,  Prev: Command Loop,  Up: Top
  767. Keymaps
  768. *******
  769.    The bindings between input events and commands are recorded in data
  770. structures called "keymaps".  Each binding in a keymap associates (or
  771. "binds") an individual event type either with another keymap or with a
  772. command.  When an event is bound to a keymap, that keymap is used to
  773. look up the next character typed; this continues until a command is
  774. found.  The whole process is called "key lookup".
  775. * Menu:
  776. * Keymap Terminology::            Definitions of terms pertaining to keymaps.
  777. * Format of Keymaps::        What a keymap looks like as a Lisp object.
  778. * Creating Keymaps::         Functions to create and copy keymaps.
  779. * Inheritance and Keymaps::    How one keymap can inherit the bindings
  780.                    of another keymap.
  781. * Prefix Keys::                 Defining a key with a keymap as its definition.
  782. * Menu Keymaps::        A keymap can define a menu.
  783. * Active Keymaps::            Each buffer has a local keymap
  784.                                    to override the standard (global) bindings.
  785.                    A minor mode can also override them.
  786. * Key Lookup::                  How extracting elements from keymaps works.
  787. * Functions for Key Lookup::    How to request key lookup.
  788. * Changing Key Bindings::       Redefining a key in a keymap.
  789. * Key Binding Commands::        Interactive interfaces for redefining keys.
  790. * Scanning Keymaps::            Looking through all keymaps, for printing help.
  791. File: elisp,  Node: Keymap Terminology,  Next: Format of Keymaps,  Up: Keymaps
  792. Keymap Terminology
  793. ==================
  794.    A "keymap" is a table mapping event types to definitions (which can
  795. be any Lisp objects, though only certain types are meaningful for
  796. execution by the command loop).  Given an event (or an event type) and a
  797. keymap, Emacs can get the event's definition.  Events include ordinary
  798. ASCII characters, function keys, and mouse actions (*note Input
  799. Events::.).
  800.    A sequence of input events that form a unit is called a "key
  801. sequence", or "key" for short.  A sequence of one event is always a key
  802. sequence, and so are some multi-event sequences.
  803.    A keymap determines a binding or definition for any key sequence.  If
  804. the key sequence is a single event, its binding is the definition of the
  805. event in the keymap.  The binding of a key sequence of more than one
  806. event is found by an iterative process: the binding of the first event
  807. is found, and must be a keymap; then the second event's binding is found
  808. in that keymap, and so on until all the events in the key sequence are
  809. used up.
  810.    If the binding of a key sequence is a keymap, we call the key
  811. sequence a "prefix key".  Otherwise, we call it a "complete key"
  812. (because no more characters can be added to it).  If the binding is
  813. `nil', we call the key "undefined".  Examples of prefix keys are `C-c',
  814. `C-x', and `C-x 4'.  Examples of defined complete keys are `X', RET,
  815. and `C-x 4 C-f'.  Examples of undefined complete keys are `C-x C-g',
  816. and `C-c 3'.  *Note Prefix Keys::, for more details.
  817.    The rule for finding the binding of a key sequence assumes that the
  818. intermediate bindings (found for the events before the last) are all
  819. keymaps; if this is not so, the sequence of events does not form a
  820. unit--it is not really a key sequence.  In other words, removing one or
  821. more events from the end of any valid key must always yield a prefix
  822. key.  For example, `C-f C-f' is not a key; `C-f' is not a prefix key,
  823. so a longer sequence starting with `C-f' cannot be a key.
  824.    Note that the set of possible multi-event key sequences depends on
  825. the bindings for prefix keys; therefore, it can be different for
  826. different keymaps, and can change when bindings are changed.  However,
  827. a one-event sequence is always a key sequence, because it does not
  828. depend on any prefix keys for its well-formedness.
  829.    At any time, several primary keymaps are "active"--that is, in use
  830. for finding key bindings.  These are the "global map", which is shared
  831. by all buffers; the "local keymap", which is usually associated with a
  832. specific major mode; and zero or more "minor mode keymaps" which belong
  833. to currently enabled minor modes.  (Not all minor modes have keymaps.)
  834. The local keymap bindings shadow (i.e., take precedence over) the
  835. corresponding global bindings.  The minor mode keymaps shadow both
  836. local and global keymaps.  *Note Active Keymaps::, for details.
  837. File: elisp,  Node: Format of Keymaps,  Next: Creating Keymaps,  Prev: Keymap Terminology,  Up: Keymaps
  838. Format of Keymaps
  839. =================
  840.    A keymap is a list whose CAR is the symbol `keymap'.  The remaining
  841. elements of the list define the key bindings of the keymap.  Use the
  842. function `keymapp' (see below) to test whether an object is a keymap.
  843.    An ordinary element is a cons cell of the form `(TYPE .  BINDING)'.
  844. This specifies one binding which applies to events of type TYPE.  Each
  845. ordinary binding applies to events of a particular "event type", which
  846. is always a character or a symbol.  *Note Classifying Events::.
  847.    A cons cell whose CAR is `t' is a "default key binding"; any event
  848. not bound by other elements of the keymap is given BINDING as its
  849. binding.  Default bindings allow a keymap to bind all possible event
  850. types without having to enumerate all of them.  A keymap that has a
  851. default binding completely masks any lower-precedence keymap.
  852.    If an element of a keymap is a vector, the vector counts as bindings
  853. for all the ASCII characters; vector element N is the binding for the
  854. character with code N.  This is a more compact way to record lots of
  855. bindings.  A keymap with such a vector is called a "full keymap".
  856. Other keymaps are called "sparse keymaps".
  857.    When a keymap contains a vector, it always defines a binding for
  858. every ASCII character even if the vector element is `nil'.  Such a
  859. binding of `nil' overrides any default binding in the keymap.  However,
  860. default bindings are still meaningful for events that are not ASCII
  861. characters.  A binding of `nil' does *not* override lower-precedence
  862. keymaps; thus, if the local map gives a binding of `nil', Emacs uses
  863. the binding from the global map.
  864.    Aside from bindings, a keymap can also have a string as an element.
  865. This is called the "overall prompt string" and makes it possible to use
  866. the keymap as a menu.  *Note Menu Keymaps::.
  867.    Keymaps do not directly record bindings for the meta characters,
  868. whose codes are from 128 to 255.  Instead, meta characters are regarded
  869. for purposes of key lookup as sequences of two characters, the first of
  870. which is ESC (or whatever is currently the value of
  871. `meta-prefix-char').  Thus, the key `M-a' is really represented as `ESC
  872. a', and its global binding is found at the slot for `a' in `esc-map'.
  873.    Here as an example is the local keymap for Lisp mode, a sparse
  874. keymap.  It defines bindings for DEL and TAB, plus `C-c C-l', `M-C-q',
  875. and `M-C-x'.
  876.      lisp-mode-map
  877.      =>
  878.      (keymap
  879.       ;; TAB
  880.       (9 . lisp-indent-line)
  881.       ;; DEL
  882.       (127 . backward-delete-char-untabify)
  883.       (3 keymap
  884.          ;; `C-c C-l'
  885.          (12 . run-lisp))
  886.       (27 keymap
  887.           ;; `M-C-q', treated as `ESC C-q'
  888.           (17 . indent-sexp)
  889.           ;; `M-C-x', treated as `ESC C-x'
  890.           (24 . lisp-send-defun)))
  891.  - Function: keymapp OBJECT
  892.      This function returns `t' if OBJECT is a keymap, `nil' otherwise.
  893.      Practically speaking, this function tests for a list whose CAR is
  894.      `keymap'.
  895.           (keymapp '(keymap))
  896.               => t
  897.           (keymapp (current-global-map))
  898.               => t
  899. File: elisp,  Node: Creating Keymaps,  Next: Inheritance and Keymaps,  Prev: Format of Keymaps,  Up: Keymaps
  900. Creating Keymaps
  901. ================
  902.    Here we describe the functions for creating keymaps.
  903.  - Function: make-keymap &optional PROMPT
  904.      This function creates and returns a new full keymap (i.e., one
  905.      which contains a vector of length 128 for defining all the ASCII
  906.      characters).  The new keymap initially binds all ASCII characters
  907.      to `nil', and does not bind any other kind of event.
  908.           (make-keymap)
  909.               => (keymap [nil nil nil ... nil nil])
  910.      If you specify PROMPT, that becomes the overall prompt string for
  911.      the keymap.  The prompt string is useful for menu keymaps (*note
  912.      Menu Keymaps::.).
  913.  - Function: make-sparse-keymap &optional PROMPT
  914.      This function creates and returns a new sparse keymap with no
  915.      entries.  The new keymap does not bind any events.  The argument
  916.      PROMPT specifies a prompt string, as in `make-keymap'.
  917.           (make-sparse-keymap)
  918.               => (keymap)
  919.  - Function: copy-keymap KEYMAP
  920.      This function returns a copy of KEYMAP.  Any keymaps which appear
  921.      directly as bindings in KEYMAP are also copied recursively, and so
  922.      on to any number of levels.  However, recursive copying does not
  923.      take place when the definition of a character is a symbol whose
  924.      function definition is a keymap; the same symbol appears in the
  925.      new copy.
  926.           (setq map (copy-keymap (current-local-map)))
  927.           => (keymap
  928.                ;; (This implements meta characters.)
  929.                (27 keymap
  930.                    (83 . center-paragraph)
  931.                    (115 . center-line))
  932.                (9 . tab-to-tab-stop))
  933.           
  934.           (eq map (current-local-map))
  935.               => nil
  936.           (equal map (current-local-map))
  937.               => t
  938.